﻿Imports TBCC.UserManage

Public Class Program

    'Private Shared AppThread As String = "{F9A63348-1CCC-4217-8683-5A5B33A282BB}"
    Private Shared AppThread As String = ""
    Private Shared WS_SHOWNORMAL As Integer = &H1
    Declare Auto Function FindWindow Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Auto Function SetForegroundWindow Lib "user32.dll" Alias "SetForegroundWindow" (ByVal handler As IntPtr) As Boolean
    Declare Auto Function ShowWindow Lib "user32.dll" Alias "ShowWindow" (ByVal handler As IntPtr, ByVal cmd As Integer) As Boolean

    Shared frmMain As New MainForm

    Public Shared Sub Main()

        Dim mutex As Threading.Mutex
        Dim rst As Boolean

        mutex = New Threading.Mutex(True, AppThread, rst)
        If (rst = False) Then
            Dim p As Process = GetRunningInstance()
            If (p IsNot Nothing) Then
                ShowMainWindow(p)
            End If
            Exit Sub
        End If

        Application.EnableVisualStyles()
        Application.DoEvents()

        'Dim splash As New SplashForm()
        'splash.ShowDialog()
        ''User.Instance.Login("a", "a")
        'Dim frmLogin As New LoginForm
        'If frmLogin.ShowDialog = DialogResult.Cancel Then
        '    Exit Sub
        'End If


        'User.Instance.Login("admin", "admin")

        'Log.log(tbccLogOperateType.Login) '''

        ' AddHandler Application.ThreadException, AddressOf Application_ThreadException

        AddHandler frmMain.FormClosed, AddressOf app_exit
        'CheckUserRight(frmMain, False, NowStatus)
        'Application.Run(frmMain)
        ' frmMain.Show()
        ' frmMain.Hide()
        'frmMain.setAlarmVisable(True)
        Select Case MainForm.readConfig("SystemType")
            Case 0
                frmMain.Timer_ServerUpload.Start()
                frmMain.Timer_FDAPExDate.Start()
            Case Else
        End Select
        mutex.ReleaseMutex()
        'frmMain.Hide()
    End Sub

    Public Shared Sub HideMainForm()
        frmMain.Hide()
    End Sub
    Public Shared IsClicked As Boolean = False
    Public Shared Sub ShowMainForm()
        frmMain.Show()
        frmMain.WindowState = FormWindowState.Maximized
        If Not IsClicked Then
            If IsHasALarm() Then
                frmMain.tsbProjects.DropDownItems(0).PerformClick()
                CheckUserRight(frmMain, False, frmMain.NowStatus)
            End If
            frmMain.IsClicked = True
            IsClicked = True
        End If
        ' CheckUserRight(frmMain, False, frmMain.NowStatus)
    End Sub

    Public Shared Sub RegShowMethod(ByVal method As ShowSelect)
        frmMain.RegShowMethod(method)
    End Sub

    Public Shared Sub SwitchUser()
        frmMain.SwitchUser()
        IsClicked = False
    End Sub

    Public Shared Function IsHasALarm() As Boolean
        Dim il As IList(Of ProjectItem)
        il = ProjectService.GetProjectList()
        If il IsNot Nothing Then
            If il.Count > 0 Then
                Return True
            End If
        End If
        Return False
    End Function

    Public Shared Function g_offline() As Boolean
        Return frmMain.IsOffline
    End Function

    'Public Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)

    '    MessageBox.Show("系统错误，请重新启动！")
    '    Application.Exit()
    'End Sub

    Public Shared Function GetRunningInstance() As Process

        Dim current As Process = System.Diagnostics.Process.GetCurrentProcess
        Dim processes() As Process = System.Diagnostics.Process.GetProcessesByName(current.ProcessName)

        For Each p As Process In processes
            If (p.Id <> current.Id) Then
                Return p
            End If
        Next
        Return Nothing
    End Function

    Public Shared Sub ShowMainWindow(ByVal p As Process)
        Dim handler As IntPtr = p.MainWindowHandle
        If (handler.ToInt32() <> 0) Then
            ShowWindow(handler, WS_SHOWNORMAL)
            SetForegroundWindow(handler)
        Else
            handler = FindWindow(Nothing, ProjectMainWindowTitile)
            ShowWindow(handler, WS_SHOWNORMAL)
            SetForegroundWindow(handler)
        End If
    End Sub

    ''' <summary>
    ''' 程序退出
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Shared Sub app_exit(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        'Log.log(tbccLogOperateType.Login, "注销") '''
    End Sub


    Public Shared Sub setTsmiVisable(ByVal tsmiItem As ToolStripMenuItem, ByVal status As Boolean)
        For Each item As Object In tsmiItem.DropDownItems
            If TypeOf item Is ToolStripMenuItem Then
                If item.DropDownItems.Count > 0 Then
                    setTsmiVisable(item, status)
                End If
                item.Visible = status
                item.Enabled = status
            End If
            If TypeOf item Is ToolStripSeparator Then
                item.Visible = status
                item.Enabled = status
            End If
        Next
    End Sub

    'online: 表示是否已经连接
    Public Shared Sub CheckUserRight(ByVal frm As MainForm, ByVal IsOnline As Boolean, ByVal Status As Boolean)

        Dim gp As System.Security.Principal.GenericPrincipal
        gp = My.User.CurrentPrincipal

        Dim IsPrjSel As Boolean = False  '工程是否选择

        If MainForm.pi IsNot Nothing Then
            IsPrjSel = True
        End If

        For Each item As Object In frm.MenuStrip1.Items
            If TypeOf item Is ToolStripMenuItem Then
                Dim tsi As ToolStripMenuItem = CType(item, ToolStripMenuItem)
                setTsmiVisable(tsi, False)
                tsi.Visible = False
                tsi.Enabled = False
            End If
        Next

        For Each item As Object In frm.ToolStrip1.Items
            item.Visible = False
        Next

        '如果工程为空
        If MainForm.pi Is Nothing Then
            frm.MenuItemProjectConfig.Visible = True
            frm.MenuItemProjectConfig.Enabled = True

            frm.mnuprjload.Visible = True
            frm.mnuprjload.Enabled = True

            frm.mnuprjsave.Visible = True
            frm.mnuprjsave.Enabled = True

            frm.ToolStripSeparator3.Visible = True

            frm.mnuprjconfig.Visible = True
            frm.mnuprjconfig.Enabled = True

            frm.submnu_connectinfo.Visible = True
            frm.submnu_connectinfo.Enabled = True

            For Each item As Object In frm.ToolStrip1.Items
                item.Visible = False
            Next
            frm.tsbProjects.Visible = True
            frm.ToolStripSeparator52.Visible = True
            frm.tsbReturn.Visible = True
            frm.tsbSwitch.Visible = True

            Exit Sub
        End If

        '[工程管理]
        'frm.MenuItemProjectConfig.Visible = True
        'frm.MenuItemProjectConfig.Enabled = True


        '[系统管理]
        frm.menuItemSysAdmin.Visible = True
        frm.menuItemSysAdmin.Enabled = True

        '设备管理
        frm.tsmiDevManage.Visible = True
        frm.tsmiDevManage.Enabled = True

        setTsmiVisable(frm.tsmiDevManage, True)

        Select Case MainForm.readConfig("SystemType")
            Case 0

                '上传管理
                If Status Then
                    frm.tsmiUploadCfg.Visible = True
                    frm.tsmiUploadCfg.Enabled = True
                Else
                    frm.tsmiUploadCfg.Visible = False
                    frm.tsmiUploadCfg.Enabled = False

                End If


                setTsmiVisable(frm.tsmiUploadCfg, True)
            Case Else
        End Select


        '[系统配置]
        'frm.MenuItemSysCfg.Visible = True
        'frm.MenuItemSysCfg.Enabled = True

        '[数据分析]
        frm.MenuItemData.Visible = True
        frm.MenuItemData.Enabled = True
        '实时数据查看
        frm.mnuMain.Visible = True
        frm.mnuMain.Enabled = True
        '
        frm.ToolStripSeparator12.Visible = True
        '实时报警查看
        frm.mnuRealtimeAlarm.Visible = True
        frm.mnuRealtimeAlarm.Enabled = True

        '[冷链文档管理]
        If Status Then
            frm.MenuItemColdAdmin.Visible = True
            frm.MenuItemColdAdmin.Enabled = True
            setTsmiVisable(frm.MenuItemColdAdmin, True)
        Else
            frm.MenuItemColdAdmin.Visible = False
            frm.MenuItemColdAdmin.Enabled = False
        End If



        frm.mnuDocConf.Visible = False
        '[通讯]
        frm.MenuItemComm.Visible = True
        frm.MenuItemComm.Enabled = True

        setTsmiVisable(frm.MenuItemComm, True)

        '[帮助]
        frm.mnuhelp.Visible = True
        frm.mnuhelp.Enabled = True
        '帮助
        frm.mnuHelpfile.Visible = True
        frm.mnuHelpfile.Enabled = True
        '关于
        frm.mnuAbout.Visible = True
        frm.mnuAbout.Enabled = True


        '/////////////////////////////////工具栏/////////////////
        frm.tsbProjects.Visible = True
        frm.ToolStripSeparator4.Visible = True
        frm.tsbLink.Visible = True
        frm.ToolStripSeparator52.Visible = True
        frm.tsbReturn.Visible = True

        frm.SetSwitchButtonVisible()
        If Status Then '仓储
            frm.ToolStripSeparator23.Visible = True
            frm.tsbHisData.Visible = True
            frm.tsbHistCurve.Visible = True
            frm.tsbHisAlarm.Visible = True
            frm.tsbHisSt.Visible = True
        End If
        '/////////////////////////////////工具栏/////////////////

        If IsOnline Then '*************在线*************
            '[工程管理]
            '测试窗体

            '[系统管理]

            '[系统配置]

            '[数据分析]

            '[冷链文档管理]

            '[通讯]
            frm.mnuReConnect.Enabled = False

            '/////////////////////////////////工具栏/////////////////
            frm.ToolStripSeparator23.Visible = True
            frm.tsbMain.Visible = True
            frm.tsbAlarm.Visible = True

            If Status Then

                Select Case MainForm.readConfig("SystemType")
                    Case 0
                        frm.ToolStripSeparator24.Visible = True
                        frm.tsddbUpload.Visible = True
                    Case Else
                End Select

            End If
            '/////////////////////////////////工具栏/////////////////
        Else '**************离线*************
            '[工程管理]

            '[系统管理]
            '设备管理
            frm.tsmiDevManage.Enabled = False
            setTsmiVisable(frm.tsmiDevManage, False)
            '手动上传
            frm.tsmiUpload.Enabled = False
            setTsmiVisable(frm.tsmiUpload, False)
            '[系统配置]

            '[数据分析]
            frm.mnuMain.Enabled = False
            frm.mnuRealtimeAlarm.Enabled = False
            '[冷链文档管理]

            '[通讯]
            frm.mnuDisconnect.Enabled = False
        End If

        '高级用户
        If gp.IsInRole(RoleClass.GetRolename(RoleType.HighLevel)) Then
            '[工程管理]
            frm.MenuItemProjectConfig.Visible = True
            frm.MenuItemProjectConfig.Enabled = True

            setTsmiVisable(frm.MenuItemProjectConfig, True)

            '工程参数配置
            frm.mnuprjconfig.Visible = True
            frm.mnuprjconfig.Enabled = True

            frm.ToolStripSeparator46.Visible = False
            frm.GPRSMenuItem.Visible = False
            '设备校准配置
            frm.mnuAIrevise.Visible = True
            frm.mnuAIrevise.Enabled = True

            '网络连接配置
            frm.mnuEthernetConfig.Visible = True
            frm.mnuEthernetConfig.Enabled = True

            '测试窗体
            frm.mnuTest.Visible = True
            frm.VersionReadMenuItem.Visible = True

            '[系统管理]
            '数据管理
            frm.UpLoadConMenuItem.Visible = True
            frm.UpLoadConMenuItem.Enabled = True


            setTsmiVisable(frm.UpLoadConMenuItem, True)




            '仓库管理
            'frm.tsmiAlarmManage.Visible = True
            'frm.tsmiAlarmManage.Enabled = True

            'setTsmiVisable(frm.tsmiAlarmManage, True)

            '制冷管理
            'frm.tsmiCcapManage.Visible = True
            'frm.tsmiCcapManage.Enabled = True

            'setTsmiVisable(frm.tsmiCcapManage, True)

            '
            frm.ToolStripSeparator8.Visible = True
            '
            frm.ToolStripSeparator9.Visible = True

            '[系统配置]
            frm.MenuItemSysCfg.Visible = True
            frm.MenuItemSysCfg.Enabled = True

            setTsmiVisable(frm.MenuItemSysCfg, True)

            '[数据分析]

            '[冷链文档管理]

            '[通讯]

            If Status Then '*************仓储*************
                '[工程管理]
                frm.tsmiCcapPrjCfg.Visible = False
                frm.mnuAIrevise.Visible = True
                frm.WebserviceToolStripMenuItem.Visible = False
                '[系统管理]
                '仓库管理
                frm.tsmiAlarmManage.Visible = True
                frm.tsmiAlarmManage.Enabled = True

                setTsmiVisable(frm.tsmiAlarmManage, True)


                '[断电数据上传]
                frm.menPowerTime.Enabled = True
                frm.menPowerTime.Visible = True


                '[系统配置]

                '[数据分析]

                '[冷链文档管理]

                frm.menPowerView.Enabled = True
                frm.menPowerView.Visible = True

                frm.FDAPsetItem.Visible = True
                frm.FDAPsetItem.Enabled = True
                frm.sLEDMenuItem.Visible = True
                frm.sLEDMenuItem.Enabled = True
                frm.LEDMenuItem.Visible = True
                frm.LEDMenuItem.Enabled = True
                setTsmiVisable(frm.FDAPsetItem, True)
                setTsmiVisable(frm.sLEDMenuItem, True)
                setTsmiVisable(frm.LEDMenuItem, True)
                'zhongxinshuj shangc 
                frm.UploadSMenuItem.Visible = True
                frm.UploadSMenuItem.Enabled = True

                '[通讯]
                If IsOnline Then '*************在线*************
                    '[工程管理]
                    '设备参数配置
                    frm.submenu_devpara.Enabled = False
                    'I/O端口配置(离线模式
                    frm.MnuIOConfigdefine_offline.Enabled = False
                    frm.MenuOfflineCold.Enabled = False

                    '测试窗体
                    frm.mnuTest.Enabled = True
                    frm.VersionReadMenuItem.Enabled = True

                    '导入
                    frm.mnuprjload.Enabled = False

                    '工程信息配置
                    frm.submnu_connectinfo.Enabled = False

                    frm.EthernetDevMenuItem.Visible = False
                    frm.ToolStripSeparator50.Visible = False

                    '制冷设备网络数量配置
                    frm.ccapNetNumMenuItem.Visible = False
                    '[系统管理]

                    '[系统配置]

                    '[数据分析]

                    '[冷链文档管理]
                    frm.FDAPsetItem.Enabled = False
                    frm.LEDMenuItem.Enabled = False
                    frm.sLEDMenuItem.Enabled = False
                    setTsmiVisable(frm.FDAPsetItem, False)
                    setTsmiVisable(frm.LEDMenuItem, False)
                    setTsmiVisable(frm.sLEDMenuItem, False)

                    '[通讯]
                Else '**************离线*************
                    '[工程管理]
                    '网络连接配置
                    frm.mnuEthernetConfig.Enabled = False
                    setTsmiVisable(frm.mnuEthernetConfig, False)
                    '设备校准配置
                    frm.mnuAIrevise.Enabled = False
                    setTsmiVisable(frm.mnuAIrevise, False)
                    '网络结构数量配置
                    frm.mnunetstruct.Enabled = False
                    setTsmiVisable(frm.mnunetstruct, False)
                    '网络地址配置
                    frm.mnunetaddr.Enabled = False
                    '拨号设备设置
                    frm.AlarmDevMenuItem.Enabled = False
                    'GPRS连接参数配置
                    frm.GPRSMenuItem.Enabled = False
                    '历史记录配置
                    frm.mnuDataUploadConfig.Enabled = False
                    'I/O端口配置(在线)
                    frm.MnuIOConfigdefine_online.Enabled = False
                    frm.DeviceNetWorkItem.Enabled = False

                    frm.MenuOnlineCold.Enabled = False
                    '兼容模块I/O驱动类型配置
                    frm.IOToolStripMenuItem.Enabled = False

                    '测试窗体
                    frm.mnuTest.Enabled = False
                    frm.VersionReadMenuItem.Enabled = False
                    '[系统管理]
                    frm.tsmiAlarmManage.Enabled = False
                    setTsmiVisable(frm.tsmiAlarmManage, False)
                    frm.UpLoadConMenuItem.Enabled = False
                    setTsmiVisable(frm.UpLoadConMenuItem, False)

                    '[系统配置]
                    frm.mnuPhoneConfig.Enabled = False
                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                End If
            Else '*************制冷*************
                '[工程管理]
                frm.tsmiAlarmPrjCfg.Visible = False
                frm.mnuAIrevise.Visible = False

                '[系统管理]
                '制冷管理
                frm.tsmiCcapManage.Visible = True
                frm.tsmiCcapManage.Enabled = True

                frm.UpLoadConMenuItem.Visible = False
                frm.tsmiUploadCfg.Visible = False

                setTsmiVisable(frm.tsmiCcapManage, True)
                '[系统配置]

                '[数据分析]

                '[冷链文档管理]

                '[通讯]
                If IsOnline Then '*************在线*************
                    '[工程管理]
                    '制冷设备配置
                    frm.CcapDevMenuItem.Enabled = False
                    '测试窗体
                    frm.mnuTest.Enabled = True
                    frm.VersionReadMenuItem.Enabled = True
                    '导入
                    frm.mnuprjload.Enabled = False
                    '工程信息配置
                    frm.submnu_connectinfo.Enabled = False

                    '网络设备设置
                    frm.EthernetDevMenuItem.Visible = False
                    frm.ToolStripSeparator50.Visible = False

                    'frm.DevExMenuItem.Visible = False
                    'frm.ToolStripSeparator49.Visible = False
                    frm.DevEx16TypeItem.Visible = False
                    '[系统管理]

                    '[系统配置]

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                Else '*************离线*************
                    '[工程管理]
                    '网络连接配置
                    frm.mnuEthernetConfig.Enabled = False
                    setTsmiVisable(frm.mnuEthernetConfig, False)
                    '设备校准配置
                    frm.mnuAIrevise.Enabled = False
                    setTsmiVisable(frm.mnuAIrevise, False)
                    '网络结构数量配置
                    frm.mnunetstruct.Enabled = False
                    setTsmiVisable(frm.mnunetstruct, False)
                    '网络地址配置
                    frm.mnunetaddr.Enabled = False
                    '拨号设备设置
                    frm.AlarmDevMenuItem.Enabled = False
                    'GPRS连接参数配置
                    frm.GPRSMenuItem.Enabled = False
                    '制冷设备端口配置
                    frm.ccapIOParameterMenuItem.Enabled = False

                    '测试窗体
                    frm.mnuTest.Enabled = False
                    frm.VersionReadMenuItem.Enabled = False
                    '[系统管理]
                    frm.tsmiCcapManage.Enabled = False
                    setTsmiVisable(frm.tsmiCcapManage, False)
                    frm.UpLoadConMenuItem.Enabled = False
                    setTsmiVisable(frm.UpLoadConMenuItem, False)
                    '[系统配置]
                    frm.mnuPhoneConfig.Enabled = False

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                End If
            End If
        End If

        '中级用户
        If gp.IsInRole(RoleClass.GetRolename(RoleType.MiddleLevel)) Then
            '[工程管理]
            frm.MenuItemProjectConfig.Visible = True
            frm.MenuItemProjectConfig.Enabled = True
            '导入导出
            frm.mnuprjload.Visible = True
            frm.mnuprjload.Enabled = True
            frm.mnuprjsave.Visible = True
            frm.mnuprjsave.Enabled = True

            '[系统管理]
            Select Case MainForm.readConfig("SystemType")
                Case 2
                    frm.UpLoadConMenuItem.Visible = False
                Case Else
                    '数据管理
                    frm.UpLoadConMenuItem.Visible = True
                    frm.UpLoadConMenuItem.Enabled = True

                    setTsmiVisable(frm.UpLoadConMenuItem, True)
            End Select
            

            '
            frm.ToolStripSeparator8.Visible = True
            '
            frm.ToolStripSeparator9.Visible = True

            '[系统配置]
            frm.MenuItemSysCfg.Visible = True
            frm.MenuItemSysCfg.Enabled = True

            setTsmiVisable(frm.MenuItemSysCfg, True)

            '[数据分析]

            '[冷链文档管理]

            '[通讯]

            If Status Then '*************仓储*************
                '[工程管理]

                '[系统管理]
                '仓库管理
                frm.tsmiAlarmManage.Visible = True
                frm.tsmiAlarmManage.Enabled = True

                setTsmiVisable(frm.tsmiAlarmManage, True)

                '[系统配置]

                '[数据分析]

                '[冷链文档管理]

                '[通讯]
                If IsOnline Then '*************在线*************
                    '[工程管理]
                    '导入
                    frm.mnuprjload.Enabled = False
                    '工程信息配置
                    frm.submnu_connectinfo.Enabled = False
                    '[系统管理]

                    '[系统配置]

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                Else '**************离线*************
                    '[工程管理]

                    '[系统管理]
                    frm.tsmiAlarmManage.Enabled = False
                    setTsmiVisable(frm.tsmiAlarmManage, False)
                    frm.UpLoadConMenuItem.Enabled = False
                    setTsmiVisable(frm.UpLoadConMenuItem, False)

                    '[系统配置]
                    frm.mnuPhoneConfig.Enabled = False

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                End If
            Else '*************制冷*************
                '[工程管理]

                '[系统管理]
                '制冷管理
                frm.tsmiCcapManage.Visible = True
                frm.tsmiCcapManage.Enabled = True

                frm.UpLoadConMenuItem.Visible = False
                frm.tsmiUploadCfg.Visible = False

                setTsmiVisable(frm.tsmiCcapManage, True)
                '[系统配置]

                '[数据分析]

                '[冷链文档管理]

                '[通讯]
                If IsOnline Then '*************在线*************
                    '[工程管理]
                    '导入
                    frm.mnuprjload.Enabled = False

                    '工程信息配置
                    frm.submnu_connectinfo.Enabled = False

                    '[系统管理]

                    '[系统配置]

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                Else '**************离线*************
                    '[工程管理]

                    '[系统管理]
                    frm.tsmiCcapManage.Enabled = False
                    setTsmiVisable(frm.tsmiCcapManage, False)
                    frm.UpLoadConMenuItem.Enabled = False
                    setTsmiVisable(frm.UpLoadConMenuItem, False)

                    '[系统配置]
                    frm.mnuPhoneConfig.Enabled = False

                    '[数据分析]

                    '[冷链文档管理]

                    '[通讯]
                End If
            End If
        End If

    End Sub


End Class

Public Delegate Sub ShowSelect()